package me.zhengjie.minbearwcs.controller.outbound;

import lombok.RequiredArgsConstructor;
import me.zhengjie.minbearwcs.entity.DTO.response.RespWithObject;
import me.zhengjie.minbearwcs.entity.DTO.transfer.Tuple;
import me.zhengjie.minbearwcs.serivce.outbound.OutboundPdaService;
import org.springframework.web.bind.annotation.*;
import me.zhengjie.minbearwcs.entity.DTO.outbound.pda.BoxMessage;
import me.zhengjie.minbearwcs.entity.DTO.outbound.pda.RequiredSortingMessage;
import me.zhengjie.minbearwcs.entity.DTO.outbound.pda.SortMessage;

import java.util.List;

@RestController
@RequestMapping("/pda/outbound")
@RequiredArgsConstructor
public class OutboundPdaController {

    private final OutboundPdaService outboundPdaService;


    // 出库单手动排序
    @PostMapping("/order/sort")
    public RespWithObject<Void> sortOutboundOrder(@RequestBody List<String> outboundOrderList) {
        outboundPdaService.sortOutboundOrder(outboundOrderList);
        return new RespWithObject<>();
    }

    // 扫描栈板号，获取分拣信息
    @GetMapping("/pallet/sort/{palletCode}")
    public RespWithObject<RequiredSortingMessage> sortOutboundPallet(@PathVariable String palletCode) {
        Tuple<RequiredSortingMessage> tuple = outboundPdaService.sortOutboundPallet(palletCode);

        if (tuple.success) {
            return new RespWithObject<RequiredSortingMessage>()
                    .data(tuple.value);
        } else {
            return new RespWithObject<RequiredSortingMessage>()
                    .code(false)
                    .reason("没找到分拣信息");
        }

    }


    // 扫描箱号，返回对应箱类型以及包含哪些小箱和物料数量
    @GetMapping("/box/num/{boxCode}")
    public RespWithObject<BoxMessage> getBoxMessage(@PathVariable String boxCode) {
        Tuple<BoxMessage> tuple = outboundPdaService.getBoxMessage(boxCode);

        if (tuple.success) {
            return new RespWithObject<BoxMessage>()
                    .data(tuple.value);
        } else {
            return new RespWithObject<BoxMessage>()
                    .code(false)
                    .reason("没找相关箱号信息");
        }
    }


    // 绑定好后，上报绑定信息
    @PostMapping("/pallet/binding")
    public RespWithObject<Void> submitSortMessage(@RequestBody SortMessage sortMessage) {
        outboundPdaService.submitSortMessage(sortMessage);
        return new RespWithObject<>();
    }


    // 分拣出库叫车
    @GetMapping("/sort/call/agv/{qrPoint}")
    public RespWithObject<String> sortAfterCallAgv(@PathVariable String qrPoint) {
        return outboundPdaService.sortAfterCallAgv(qrPoint);
    }


    // 搬走栈板后，清空储位信息
    @GetMapping("/storage/clear/{storageCode}")
    public RespWithObject<String> clearStorage(@PathVariable String storageCode) {
        Tuple<String> tuple = outboundPdaService.clearStorage(storageCode);
        if (tuple.success) {
            return new RespWithObject<String>()
                    .reason("储位清除成功");
        } else {
            return new RespWithObject<String>()
                    .code(false)
                    .reason(tuple.value);
        }
    }


    @GetMapping("/sort/exception/call/agv/{palletCode}")
    public RespWithObject<String> sortExceptionCallAgv(@PathVariable String palletCode) {
        Tuple<String> stringTuple = outboundPdaService.sortExceptionCallAgv(palletCode);
        return new RespWithObject<String>()
                .code(stringTuple.success)
                .reason(stringTuple.value);
    }


    @GetMapping("/sort/continue/call/agv/{palletCode}")
    public RespWithObject<String> sortContinueCallAgv(@PathVariable String palletCode) {
        Tuple<String> stringTuple = outboundPdaService.sortContinueCallAgv(palletCode);
        return new RespWithObject<String>()
                .code(stringTuple.success)
                .reason(stringTuple.value);
    }


}
